package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    GraphInfoService graphInfoService;
    @Autowired
    ApartmentFacilityService apartmentFacilityService;
    @Autowired
    ApartmentLabelService apartmentLabelService;
    @Autowired
    ApartmentFeeValueService feeValueService;
    @Autowired
    ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    LabelInfoMapper labelInfoMapper;
    @Autowired
    FacilityInfoMapper facilityInfoMapper;
    @Autowired
    FeeValueMapper feeValueMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {

        boolean b = apartmentSubmitVo.getId() != null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (b) {
            //删除的是单值
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);

            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            feeValueService.remove(apartmentFeeValueLambdaQueryWrapper);


        }
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            BeanUtils.copyProperties(graphVo, graphInfo);
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfos.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfos);//进行批处理存储
        //2.插入配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
        facilityInfoIdList.forEach(ApartmentFacilityId -> {

            ApartmentFacility apartmentFacility = new ApartmentFacility();
            apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
            apartmentFacility.setFacilityId(ApartmentFacilityId);
            facilityList.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(facilityList);//进行批处理存储
        //3.插入标签列表
        apartmentSubmitVo.getLabelIds().stream().
                map(labelid -> ApartmentLabel.builder().
                        apartmentId(apartmentSubmitVo.getId()).labelId(labelid).build()).
                forEach(apartmentLabel -> apartmentLabelService.saveOrUpdate(apartmentLabel));
//        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
//        ArrayList<ApartmentLabel> labelList = new ArrayList<>();
//        labelIds.forEach(labelId -> {
//            ApartmentLabel apartmentLabel = new ApartmentLabel();
//            apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
//            apartmentLabel.setLabelId(labelId);
//            labelList.add(apartmentLabel);
//        });
//        apartmentLabelService.saveBatch(labelList);
        //4.插入杂费列表

        apartmentSubmitVo.getFeeValueIds().stream().map(feevalueId ->
                        //建造者模式用于设置值
                        ApartmentFeeValue.builder().apartmentId(apartmentSubmitVo.getId()).feeValueId(feevalueId).build())

                .forEach(apartmentFeeValue -> feeValueService.saveOrUpdate(apartmentFeeValue));

//        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
//        ArrayList<ApartmentFeeValue> feeValueList = new ArrayList<>();
//        feeValueIds.forEach(feeValueId -> {
//            ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
//            apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
//            apartmentFeeValue.setFeeValueId(feeValueId);
//            feeValueList.add(apartmentFeeValue);
//        });
//        feeValueService.saveBatch(feeValueList);
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> objectPage, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(objectPage, queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.getApartmentDetailById(id);
        List<GraphVo> graphVoList = apartmentInfoMapper.selectListByItemTypeAndId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        List<FacilityInfo> facilityInfoList = apartmentFacilityService.getFacilityInfoById(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;

    }

    @Override
    public void removeApartmentById(Long id) {
        Long l = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
        System.out.println(l);

        if (l > 0) {
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }

        boolean b = super.removeById(id);//删除公寓信息


        //删除关联表中的信息
//        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
//        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
//        graphQueryWrapper.eq(GraphInfo::getItemId, id);
//        graphInfoService.remove(graphQueryWrapper);

//        graphInfoMapper.deleteById(id);
        graphInfoService.remove
                (new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id)
                        .eq(GraphInfo::getItemType, ItemType.APARTMENT));
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id));
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId, id));
        feeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId, id));
    }

    @Override
    public ApartmentInfo selectById(Long apartmentId) {
        ApartmentInfo byId = super.getById(apartmentId);
        return byId;
    }


}




